package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsDeliveryMissionMapper;
import me.zhengjie.mapper.WcsThreeFloorOutboundDetailsMapper;
import me.zhengjie.pojo.dto.ThreeFloorDeliveryProductionLineDto;
import me.zhengjie.pojo.entity.WcsDeliveryMission;
import me.zhengjie.pojo.entity.WcsThreeFloorOutboundDetails;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.sqlservice.WcsDeliveryMissionService;
import me.zhengjie.utility.state.TaskStatus;
import me.zhengjie.utility.state.TaskType;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
public class WcsDeliveryMissionServiceImpl extends ServiceImpl<WcsDeliveryMissionMapper, WcsDeliveryMission> implements WcsDeliveryMissionService {
    private final WcsDeliveryMissionMapper wcsDeliveryMissionMapper;
    private final WcsThreeFloorOutboundDetailsMapper wcsThreeFloorOutboundDetailsMapper;

    @Override
    public boolean insertDelivery(WcsDeliveryMission wcsDeliveryMission) {
        return wcsDeliveryMissionMapper.insert(wcsDeliveryMission) >0;
    }

    @Override
    public WcsDeliveryMission selectDeliveryCode(String deliveryCode) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsDeliveryMission::getDeliveryCode,deliveryCode);
        return wcsDeliveryMissionMapper.selectOne(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateDeliveryStatusAndAgvId(Integer id, Integer missionStatus, String agvId) {
        LambdaUpdateWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsDeliveryMissionLambdaUpdateWrapper.set(WcsDeliveryMission::getMissionStatus,missionStatus)
                .set(WcsDeliveryMission::getAgvId,agvId)
                .set(WcsDeliveryMission::getUpdateTime, LocalDateTime.now())
                .eq(WcsDeliveryMission::getId,id);
        return wcsDeliveryMissionMapper.update(null,wcsDeliveryMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateMissionStatus(String deliveryCode, Integer status) {
        LambdaUpdateWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsDeliveryMissionLambdaUpdateWrapper.set(WcsDeliveryMission::getMissionStatus,status)
                .set(WcsDeliveryMission::getUpdateTime,LocalDateTime.now())
                .eq(WcsDeliveryMission::getDeliveryCode,deliveryCode);
        return wcsDeliveryMissionMapper.update(null,wcsDeliveryMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean deleteDeliveryId(Integer id) {
        return wcsDeliveryMissionMapper.deleteById(id) >0;
    }

    @Override
    public List<WcsDeliveryMission> selectMissionStatus(Integer status) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsDeliveryMission::getMissionStatus,status);
        return wcsDeliveryMissionMapper.selectList(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsDeliveryMission> selectContainerCodeNoTask(String containerCode) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsDeliveryMission::getContainerCode,containerCode)
                .ne(WcsDeliveryMission::getMissionStatus, TaskStatus.Task_complete.getStatus())
                .ne(WcsDeliveryMission::getMissionStatus, TaskStatus.Task_cancel.getStatus());
        return wcsDeliveryMissionMapper.selectList(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsDeliveryMission> selectDestinationSNotMissionStatus(Set<String> storageCodeSet, Integer status) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.in(WcsDeliveryMission::getDestination,storageCodeSet)
                .ne(WcsDeliveryMission::getMissionStatus,status);
        return wcsDeliveryMissionMapper.selectList(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public ThreeFloorDeliveryProductionLineDto myMethod(String deliveryCode, int type) {
        ThreeFloorDeliveryProductionLineDto threeFloorDeliveryProductionLineDto = new ThreeFloorDeliveryProductionLineDto();
        List<String> containerCodeList =new ArrayList<>();
        //根据某个形参条件拿到deliver表的栈板号
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper1.eq(WcsDeliveryMission::getDeliveryCode, deliveryCode);
        WcsDeliveryMission wcsDeliveryMission = wcsDeliveryMissionMapper.selectOne(wcsDeliveryMissionLambdaQueryWrapper1);
        String containerCode = wcsDeliveryMission.getContainerCode();
        //第delivery表的containercode是varchar，明细表是int
        int palletCode = Integer.parseInt(containerCode);

        //再根据栈板号在出库明细表拿到该栈板号对应的箱子号
        LambdaQueryWrapper<WcsThreeFloorOutboundDetails> wcsDeliveryMissionLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper2.eq(WcsThreeFloorOutboundDetails::getPalletCode, palletCode);
        List<WcsThreeFloorOutboundDetails> wcsThreeFloorOutboundDetailsList = wcsThreeFloorOutboundDetailsMapper.selectList(wcsDeliveryMissionLambdaQueryWrapper2);

//        List<WcsThreeFloorOutboundDetails> wcsThreeFloorOutboundDetailsList = wcsThreeFloorOutboundDetailsMapper.selectContainerCodeByPalletCode(palletCode);

        for(WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails : wcsThreeFloorOutboundDetailsList)
        {
            containerCodeList.add(wcsThreeFloorOutboundDetails.getContainerCode());
        }
        String workOrder = wcsThreeFloorOutboundDetailsList.get(0).getWorkOrder();
        threeFloorDeliveryProductionLineDto.setContainerCodeList(containerCodeList);
        threeFloorDeliveryProductionLineDto.setWorkOrder(workOrder);
        threeFloorDeliveryProductionLineDto.setType(type);
        return threeFloorDeliveryProductionLineDto;



    }
}
